package com.ruoyi.web.controller.basedata;

import com.ruoyi.basedata.domain.DwAreas;
import com.ruoyi.basedata.service.IDwAreasService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 区域Controller
 * 
 * @author ruoyi
 * @date 2023-09-06
 */
@RestController
@RequestMapping("/basedata/areas")
public class DwAreasController extends BaseController
{
    @Autowired
    private IDwAreasService dwAreasService;

    /**
     * 查询区域列表
     */
    @PreAuthorize("@ss.hasPermi('basedata:areas:list')")
    @GetMapping("/list")
    public AjaxResult list(DwAreas dwAreas)
    {
        List<DwAreas> list = dwAreasService.selectDwAreasList(dwAreas);
        return success(list);
    }



    @PreAuthorize("@ss.hasPermi('basedata:areas:list')")
    @GetMapping("/listchildren")
    public TableDataInfo childrenList(@RequestParam(value = "id",required = false) Long id,@RequestParam(value = "areaName",required = false) String areaName)
    {
        startPage();
        List<DwAreas> list = dwAreasService.childrenList(id,areaName);
        return getDataTable(list);
    }

    /**
     * 导出区域列表
     */
    @PreAuthorize("@ss.hasPermi('basedata:areas:export')")
    @Log(title = "区域", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DwAreas dwAreas)
    {
        List<DwAreas> list = dwAreasService.selectDwAreasList(dwAreas);
        ExcelUtil<DwAreas> util = new ExcelUtil<DwAreas>(DwAreas.class);
        util.exportExcel(response, list, "区域数据");
    }

    /**
     * 获取区域详细信息
     */
    @PreAuthorize("@ss.hasPermi('basedata:areas:query')")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID)
    {
        return success(dwAreasService.selectDwAreasByID(ID));
    }

    /**
     * 新增区域
     */
    @PreAuthorize("@ss.hasPermi('basedata:areas:add')")
    @Log(title = "区域", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DwAreas dwAreas)
    {
        return toAjax(dwAreasService.insertDwAreas(dwAreas));
    }

    /**
     * 修改区域
     */
    @PreAuthorize("@ss.hasPermi('basedata:areas:edit')")
    @Log(title = "区域", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DwAreas dwAreas)
    {
        int flag = dwAreasService.updateDwAreas(dwAreas);
        if (flag == 0){
            return error("不能将自身作为父节点");
        }
        return toAjax(flag);
    }

    /**
     * 删除区域
     */
    @PreAuthorize("@ss.hasPermi('basedata:areas:remove')")
    @Log(title = "区域", businessType = BusinessType.DELETE)
	@DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs)
    {
        int flag = dwAreasService.deleteDwAreasByIDs(IDs);
        if (flag == 0){
            return error("该区域下存有子区域或楼栋");
        }
        return toAjax(flag);
    }

    @PreAuthorize("@ss.hasPermi('basedata:areas:list')")
    @GetMapping(value = "/tree")
    public AjaxResult getAreaTree()
    {
        return success(dwAreasService.getAreaTree());
    }
}
